﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Job.Entity;
using Job.Web.Framework;
using Job.Logic.Common;
using Job.Logic.Company;

namespace Job.Web.Common
{
    public partial class CompanyView : BasePage
    {
        private int CompanyId
        {
            get 
            {
                if ( myR["company"].IntValue > 0 ) return myR["company"].IntValue; 

                MySession session = new MySession();
                if (session.GetCompanyFromSession() == null)
                    return -1;
                else
                    return session.GetCompanyFromSession().CompanyId;

            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            this.msgTd.Visible = false;
            MySession session = new MySession();
            if (session.GetCompanyFromSession() == null)
            {
                ReturnError("您无权限查看企业资料");
                return;
            }
            if (!IsPostBack)
            {
                this.btDelete.Attributes.Add("onClick", "return confirm('是否确认删除企业资料？')");
                LoadData();
            }

        }

        private void LoadData()
        {            
            if (CompanyId <= 0)
            {
                ReturnError("请指定需要查看的企业");
                return;
            }

            try
            {
                JobCompanyInfo info = new CompanyLogic().GetCompanyInfo(CompanyId);
                if (null == info)
                {
                    ReturnError("查询不到企业资料！");
                    return;
                }
                else
                    ShowInfo(info);
            }
            catch (Exception ex)
            {
                ReturnError("系统内部错误");
                return;
            }
        }

        private void ReturnError(string message)
        {
            Response.Write("<script>");
            Response.Write(" alert('" + message + " ！');");
            Response.Write(" location.href='" 
                + WebCommon.GetApplicationPath(Request) + "Index.aspx'");
            Response.Write("</script>");
            return;
        }

        private void ShowInfo(JobCompanyInfo info)
        {
            lbCompanyId.Text = info.CompanyInfoId.ToString();
            lbCompanyName.Text = info.CompanyName;
            if ( info.CompanyLogo != null ) 
            { 
                imgLogo.ImageUrl = WebCommon.GetApplicationPath(Request)
                        + "Handler/GetCompanyLogo.aspx?company=" 
                        + info.CompanyInfoId + "&type=" + info.LogoExtName;
            }

            MySession session = new MySession();
            if ( session.GetAdminUserFromSession() != null )
            {
                lbLoginName.Text = info.LoginName;
                lbPassword.Text = info.LoginPswd;
            }
            else
            {
                adminTr0.Visible = false;
                adminTr1.Visible = false;
            }
            
            if ( info.CompanyProperty.HasValue) lbProperty.Text = MemoryDictionary.GetCommonDictName(info.CompanyProperty.Value);
            if ( info.CompanyScope.HasValue) lbScope.Text = MemoryDictionary.GetCommonDictName(info.CompanyScope.Value);
            lbIndustry.Text = MemoryDictionary.GetFullIndustryName(info.CompanyIndustry);
            lbFunds.Text = (null == info.RegistFunds) ? "" : info.RegistFunds;
            lbCorporation.Text = (null == info.CompanyCorporation) ? "" : info.CompanyCorporation;
            if ( info.RegistDate.HasValue) lbRegistDate.Text = Functions.GetDateStr(info.RegistDate.Value);
            lbLinkman.Text = (null == info.ContactMan) ? "" : info.ContactMan;
            lbPhone.Text = (null == info.ContactPhone) ? "" : info.ContactPhone;
            if (null != info.ContactMobile) lbPhone.Text = lbPhone.Text + "(" + info.ContactMobile + ")";

            lbEmail.Text = (null == info.ContactEmail) ? "" : info.ContactEmail;
            lbFax.Text = (null == info.ContactFax) ? "" : info.ContactFax;
            if ( info.HomePage != null && info.HomePage.Length > 0 ) 
                hlHomepage.NavigateUrl = info.HomePage;
            else
                hlHomepage.Visible = false;
         
            lbAddr.Text = MemoryDictionary.GetFullAddrName(info.CompanyAddr);
            lbTitle.Text = (null == info.CompanyTitles) ? "" : info.CompanyTitles;
            string addr = "";
            if ( info.ContactAddr != null ) addr = info.ContactAddr;
            if ( info.ContactPostcode != null ) addr = addr + " (" + info.ContactPostcode + ")";
            lbLinkAddr.Text = addr;
            
            if ( null != info.CompanyIntro) 
                lbCompanyIntro.Text = Functions.HtmlEncode(info.CompanyIntro);
            if ( null != info.ProductIntro)
                lbProductIntro.Text = Functions.HtmlEncode(info.ProductIntro);

            if (info.AccountStatus.Value) this.btAuth.Visible = false;

        }



        private void ShowError(string message)
        {
            this.msgTd.InnerHtml = "<font color='red'>" + message + "</font>";
            this.msgTd.Visible = true;
            this.msgTd.VAlign = "middle";
        }

        protected void btAuth_Click(object sender, EventArgs e)
        {
            MySession session = new MySession();
            if (session.GetAdminUserFromSession() == null)
            {
                ShowError("您无权审核企业资料！");
            }
            else
            {
                try
                {
                    new CompanyLogic().SetStatus(CompanyId, true);
                    ShowError("企业的状态已经被设置为审核通过！");
                    this.btAuth.Visible = false;
                }
                catch (Exception ex)
                {
                    ShowError("审核企业资料失败：" + ex.Message);
                }

            }
        }

        protected void btDelete_Click(object sender, EventArgs e)
        {
            MySession session = new MySession();
            if (session.GetAdminUserFromSession() == null)
            {
                ShowError("您无权删除企业资料！");
            }
            else
            {
                try
                {
                    new CompanyLogic().Delete(CompanyId);
                    ReturnError("删除企业资料成功！");
                }
                catch (Exception ex)
                {
                    ShowError("删除企业资料失败：" + ex.Message);
                }
            }
        }
    }
}
